from HTMLParser import HTMLParser
from urllib import urlopen
from datetime import date
from datetime import datetime
from lt.vincnetas.melina.model.document import Document

import codecs 

import re




class WebParser :
    
    def getDocuments(self, date):
        parser = SearchResultHTMLParser()
        return parser.getContent(date)
    
    def getTypes(self):
        return []
        
class SearchResultHTMLParser(HTMLParser):

    def __init__(self):
        self.state = 'none'        
        self.element = {}
        self.content = []
        
        self.reset()

    def getPage(self, page, queryDate):        
        reader = codecs.getreader("cp1257")    
        url = "http://www3.lrs.lt/pls/inter3/dokpaieska.rezult_l?p_nr=&p_nuo=" + queryDate.isoformat() + "&p_iki=" + queryDate.isoformat() + "&p_no=" + str(page)
        
        return reader(open("melina/testData/p" + str(page) + ".html")).read()
        return reader(urlopen(url)).read()
    
    def getContent(self, queryDate):
        page = 1
        
        while True :
            content = self.getPage(page, queryDate)
            content = content.replace(' nowrap', '')        
            elements = int(content[content.find(" viso - <b>") + 11:content.find("</b>. Paie")])        
            content = content[content.find("<table class="):]
            
            self.feed(content)
            self.reset()
            
            if elements == 0 or self.lastNumber >= elements : break
            page = page + 1
            
        return self.content
            
    def handle_data(self, data):
        if self.state == 'name' :
            self.element["name"] = data
        elif self.state == 'date' :                        
            self.element["date"] = datetime.strptime(data, "%Y-%m-%d").date()
        elif self.state == 'number' :
            self.lastNumber = int(data)
            self.element["number"] = int(data)
        elif self.state == 'type' :
            document = Document(
                title = self.element["name"],
                type = data,
                date = self.element["date"],                
                id = 12
            )
            
            self.content.append(document)
            self.element = {}

        self.state = 'none'    
    
    def handle_starttag(self, tag, attrs):        
        if self.state == 'none' :            
            if tag == 'span' and len(attrs) > 0:
                classType = attrs[0][1] 
                if classType == 'dpav' :
                    self.state = 'name'
                elif classType == 'dtip' :
                    self.state = 'type'
                elif classType == 'deilnr' :
                    self.state = 'number'
                elif classType == 'ddat' :
                    self.state = 'date'